package com.bigfish.gbs.controller;


import java.util.List;


import com.bigfish.gbs.dto.Account;
import com.bigfish.gbs.dto.Result;
import com.bigfish.gbs.entity.Book;

import com.bigfish.gbs.service.AccountService;
import com.bigfish.gbs.service.BookService;
import com.bigfish.gbs.shiro.provider.AccountProvider;
import com.bigfish.gbs.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Controller
@RequestMapping("/book") // url:/模块/资源/{id}/细分 /seckill/list
@Api(value="book controller",description="book 相关操作")
public class BookController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BookService bookService;
	@Resource
	private RedisTemplate redisTemplate;
	@Autowired
	private AccountProvider accountProvider;
	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	private String list(Model model) {
		List<Book> list = bookService.getList();
		model.addAttribute("list", list);
		// list.jsp + model = ModelAndView
		return "list";// WEB-INF/jsp/"list".jsp
	}

	@RequestMapping(value = "/{bookId}/detail", method = RequestMethod.GET)
	private String detail(@PathVariable("bookId") Long bookId, Model model) {
		if (bookId == null) {
			return "redirect:/book/list";
		}
		Book book = bookService.getById(bookId);
		if (book == null) {
			return "forward:/book/list";
		}
		model.addAttribute("book", book);
		return "detail";
	}

	@ApiOperation(value = "获取book列表",notes = "GET方式获取book列表")
	@GetMapping(value = "/list2")
	@ResponseBody
	private Result test() {
		List<Book> list = bookService.getList();
		logger.info("[Logger test] {}", list);
        redisTemplate.opsForValue().set("test", "redis test");      //redis 测试
        System.out.println(redisTemplate.opsForValue().get("test"));

		Account account = accountProvider.loadAccount("test4");
		System.out.println(account.getPassword());

		Account account1 = accountService.loadAccount("test4");
		System.out.println(account1.getPassword());

		return ResultUtil.success().addData("book", list);
	}

	/** 测试异常 */
	@GetMapping(value = "/ex")
	@ResponseBody
	private String testEx() {
		bookService.testException();
		logger.info("[test exception] {}","1111111111");
		return "test";
	}

	@RequestMapping("/index")
	@ResponseBody
	public String index(@RequestParam String id){
		System.out.println(id);
		return id;
	}

//	// ajax json
//	@RequestMapping(value = "/{bookId}/appoint", method = RequestMethod.POST, produces = {
//			"application/json; charset=utf-8" })
//	@ResponseBody
//	private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId, @RequestParam("studentId") Long studentId) {
//		if (studentId == null || studentId.equals("")) {
//			return new Result<>(false, "学号不能为空");
//		}
//		AppointExecution execution = null;
//		try {
//			execution = bookService.appoint(bookId, studentId);
//		} catch (NoNumberException e1) {
//			execution = new AppointExecution(bookId, AppointStateEnum.NO_NUMBER);
//		} catch (RepeatAppointException e2) {
//			execution = new AppointExecution(bookId, AppointStateEnum.REPEAT_APPOINT);
//		} catch (Exception e) {
//			execution = new AppointExecution(bookId, AppointStateEnum.INNER_ERROR);
//		}
//		return new Result<AppointExecution>(true, execution);
//	}


}
